//
//  ModuleWriter.m
//  CocoaSynth
//
//  Created by Simon André on 18/02/10.
//  Copyright 2010 __MyCompanyName__. All rights reserved.
//

#import <AudioToolbox/AudioToolbox.h>
#import "ModuleWriter.h"
#import "System.h"
#import "AudioOutputProtocol.h"
#import "WavOutput.h"



@implementation ModuleWriter
@synthesize path;

-(id) init{
	if (self = [super init]) {
		NSMutableArray* datas = [[NSMutableArray alloc] init];
		datasToWrite = datas;
		NSString* defaultPath = @"/Users/ninjamadak/Documents/Master2/STL/wavResult.wav";
		path = [defaultPath retain];
		[defaultPath release];
	}
	return self;
}

- (void)read{
	[super read];
	[datasToWrite addObjectsFromArray:[[self inputSample] voltages]];
}

- (void) stop{
	if ((datasToWrite != nil) && ([datasToWrite count] > 0)) {
		id<AudioOutputProtocol> audioOutput = [[WavOutput alloc] init];
		if(![audioOutput recordSignal:datasToWrite InFile:path]){
			NSLog(@"%@",@"failed to record in wav file");
		}
		[audioOutput release];
	}
	[datasToWrite release];
	NSMutableArray* datas = [[NSMutableArray alloc] init];
	datasToWrite = datas;
}

- (void)dealloc 
{
	[datasToWrite release];
	[path release];
	[super dealloc];
}

@end
